package com.watchme.license;

import java.util.Map;

import com.watchme.license.create.CreateLicense;
import com.watchme.license.verify.VerifyLicense;

public class DsLicenseUtil {

	private static DsLicenseUtil instance = null;
	private static Integer LOCK = 0;

	private DsLicenseUtil() {

	}

	public static DsLicenseUtil getInstance() {
		synchronized (LOCK) {
			if (instance == null) {
				instance = new DsLicenseUtil();
			}
			return instance;
		}
	}

	/**
	 * 创建临时lisence
	 * 
	 * @param licPath
	 * @return
	 */
	public String createTempLicense(String licPath) {
		if (licPath == null || "".equals(licPath.trim())) {
			return "licPath 不能为空";
		}
		CreateLicense cLicense = new CreateLicense();
		cLicense.setTempParam(licPath);
		return cLicense.create();
	}

	public String createLicense(String licPath, String ipAddress, String notBefore, String notAfter, String licenseId) {
		return createLicense(licPath, ipAddress, notBefore, notAfter, licenseId, null);
	}

	public String createLicense(String licPath, String ipAddress, String notBefore, String notAfter, String licenseId, String macAddress) {
		if (licPath == null || "".equals(licPath.trim())) {
			return "licPath 不能为空";
		}
		if (ipAddress == null || "".equals(ipAddress.trim())) {
			return "IP 不能为空";
		}
		CreateLicense cLicense = new CreateLicense();
		cLicense.setParam(licPath, ipAddress, macAddress, notBefore, notAfter, licenseId);
		return cLicense.create();
	}

	public boolean verifyLicense(Map<String, String> map) {
		boolean result = false;
		VerifyLicense vLicense = new VerifyLicense();
		try {
			vLicense.setParam();
			result = vLicense.verify(map);
		} catch (Exception er) {
			er.printStackTrace();
		}
		return result;
	}

}
